package com.stox.charting.ui.swing.view;

import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.util.List;

import com.stox.charting.ui.swing.model.price.PriceChart;
import com.stox.core.model.Bar;
import com.stox.core.model.IBar;
import com.stox.core.model.intf.HasBars;
import com.stox.core.ui.swing.widget.ListTransferable;

public class ChartingDropTargetListener extends DropTargetAdapter {

	protected ChartingInternalFrameView view;
	private DropTarget dropTarget;

	public ChartingDropTargetListener(ChartingInternalFrameView view) {
		this.view = view;
		dropTarget = new DropTarget(view, DnDConstants.ACTION_COPY, this, true,
				null);
	}

	@Override
	public void drop(DropTargetDropEvent event) {
		try {
			Transferable tr = event.getTransferable();
			List<?> list = (List<?>) tr
					.getTransferData(ListTransferable.listFlavor);
			if (event.isDataFlavorSupported(ListTransferable.listFlavor)) {
				event.acceptDrop(DnDConstants.ACTION_COPY);
				System.out.println(list);
				
				if(list != null){
					for(Object object : list){
						if(object instanceof HasBars){
							PriceChart<? extends IBar> chart= new PriceChart<Bar>();
							HasBars hasBars = (HasBars)object;
							view.getPriceChartView().addChart(chart);
							chart.getModel().setHasBars(hasBars);
						}
					}
				}
				
				
				
				event.dropComplete(true);
				return;
			}
			event.rejectDrop();
		} catch (Exception e) {
			e.printStackTrace();
			event.rejectDrop();
		}
	}

	public DropTarget getDropTarget() {
		return dropTarget;
	}
}
